package com.zprmb.udzt.adapter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.List;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.Mycoupon;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class CouponAdapter extends BaseAdapter {
	private List<Mycoupon> list;
	private Context context;
	private DecimalFormat df = new DecimalFormat("###0.0");
	private Dialog dialog;
	private SharedPreferences sha;
	private OnDialogListener onDialogListener;
	public CouponAdapter(List<Mycoupon> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Help h=null;
		if(convertView==null){
			h=new Help();
			convertView=LayoutInflater.from(context).inflate(R.layout.coupon_item, null);
			h.tv_name=(TextView)convertView.findViewById(R.id.name);
			h.tv_rate=(TextView)convertView.findViewById(R.id.rate);
//			h.tv_status=(TextView)convertView.findViewById(R.id.status);
			h.tv_time=(TextView)convertView.findViewById(R.id.end_time);
			h.li_bg=(LinearLayout)convertView.findViewById(R.id.li_bg);
			h.ck=(CheckBox)convertView.findViewById(R.id.ck);
			h.tv_use_ground=(TextView)convertView.findViewById(R.id.use_ground);
			h.li_ground=(LinearLayout)convertView.findViewById(R.id.li_ground);
//			h.ground=(TextView)convertView.findViewById(R.id.ground);
			h.baifen=(TextView)convertView.findViewById(R.id.baifen);
			h.img_get=(ImageView)convertView.findViewById(R.id.img_get);
//			h.coupon_status=(TextView)convertView.findViewById(R.id.coupon_status);
			convertView.setTag(h);
		}else{
			h=(Help) convertView.getTag();
		}
		
		h.img_get.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showUpVersionDialog("", "", false, "",position);
			}
		});
		h.li_ground.setVisibility(View.VISIBLE);
		h.ck.setVisibility(View.GONE);
		if(list.get(position).getState().equals("未使用")){
			list.get(position).setIsstatus(true);
			h.img_get.setVisibility(View.VISIBLE);
		}else{
			list.get(position).setIsstatus(false);
			h.img_get.setVisibility(View.GONE);
		}
		if(list.get(position).isIsstatus()){
			h.li_bg.setBackgroundResource(R.drawable.coupon);
			h.tv_rate.setTextColor(Color.parseColor("#F44947"));
			h.baifen.setTextColor(Color.parseColor("#f84746"));
			h.tv_status.setTextColor(Color.parseColor("#666666"));
			h.tv_name.setTextColor(Color.parseColor("#333333"));
			if(list.get(position).getTransferUserId()!=null&&!list.get(position).getTransferUserId().equals("null")){
				h.coupon_status.setVisibility(View.VISIBLE);
				h.coupon_status.setTextColor(Color.parseColor("#f00000"));
				h.img_get.setVisibility(View.GONE);
			}else{
				h.coupon_status.setVisibility(View.GONE);
			}
			if(list.get(position).getIsBindCardRate()==1){
				h.img_get.setVisibility(View.GONE);
			}
			if(list.get(position).getTransferStatus()==1){
				h.img_get.setVisibility(View.GONE);
			}
			
		}else{
			if(list.get(position).getTransferStatus()==2){
				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.yizhuanzeng)));				
//				h.li_bg.setBackgroundResource(R.drawable.yizhuanzeng);
			}else {
				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.yishiyong)));
//				h.li_bg.setBackgroundResource(R.drawable.yishiyong);
			}
			if(list.get(position).getState().equals("已过期")){
				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.yiguoqi)));
//				h.li_bg.setBackgroundResource(R.drawable.yiguoqi);
			}
			if(list.get(position).getTransferUserId()!=null&&!list.get(position).getTransferUserId().equals("null")){
				h.coupon_status.setVisibility(View.VISIBLE);
				h.coupon_status.setTextColor(Color.parseColor("#CCCCCC"));
				
			}
			h.tv_rate.setTextColor(Color.parseColor("#CCCCCC"));
			h.tv_time.setTextColor(Color.parseColor("#CCCCCC"));
			h.tv_status.setTextColor(Color.parseColor("#CCCCCC"));
			h.baifen.setTextColor(Color.parseColor("#CCCCCC")); 
			h.tv_use_ground.setTextColor(Color.parseColor("#CCCCCC"));
			h.ground.setTextColor(Color.parseColor("#CCCCCC"));
			h.tv_name.setTextColor(Color.parseColor("#CCCCCC"));
			
		}
		
		h.tv_name.setText(list.get(position).getRaisingRatesName());
		h.tv_rate.setText(df.format(Double.parseDouble(list.get(position).getRaisingRates())));
		h.tv_status.setText(list.get(position).getMinAmount()+"≤限投金额≤"+list.get(position).getMaxAmount());
		h.tv_time.setText("有效期至:"+list.get(position).getEndTimeShow());
		h.tv_use_ground.setText(list.get(position).getAllowProductsName());
		return convertView;
	}
	private static class Help{
		private TextView tv_name,tv_status,tv_rate,tv_time,tv_use_ground,ground,baifen,coupon_status;
		private LinearLayout li_bg,li_ground;
		private ImageView img_get;
		private CheckBox ck;
	}
	private void showUpVersionDialog(String content, final String url,
			final boolean forceUpdate,String version, final int position) {
		sha = context.getSharedPreferences("config", context.MODE_PRIVATE);
		dialog = new Dialog(context);
		dialog.setCancelable(true);
		Window window = dialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(
				R.color.float_transparent)));
		window.setGravity(Gravity.CENTER);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		window.setContentView(R.layout.coupon_get);
		TextView tv_get=(TextView)window.findViewById(R.id.tv_upversion_dialog_up);
		ImageView tv_upversion_dialog_clean=(ImageView)window.findViewById(R.id.tv_upversion_dialog_clean);
		final TextView tv_comment=(TextView)window.findViewById(R.id.tv_comment);
		final EditText et_mobile=(EditText)window.findViewById(R.id.et_mobile);
		tv_upversion_dialog_clean.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		et_mobile.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(s.length()>0){
					et_mobile.setBackgroundResource(R.drawable.ed_rf);
					tv_comment.setVisibility(View.GONE);
				}
			}
		});
		tv_get.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				// TODO Auto-generated method stub
				Activity activity = (Activity) context;
				AjaxParams params=new AjaxParams();
				try {
					params.put("mobile",  URLDecoder.decode(et_mobile.getText().toString(),"utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				params.put("id", list.get(position).getRaisingRatesId());
				
				VolleryUtil volleryUtil=new VolleryUtil() {
					
					@Override
					public void onUpDate(JSONObject jsonObject) {
						Log.e("赠送", jsonObject.toString());
						// TODO Auto-generated method stub
						if(jsonObject.optInt("success")==1){
							JSONObject json=jsonObject.optJSONObject("data");
							showUpVersionDialog2(json.optString("mobile"),"", false, "", 0);
						}else{
							tv_comment.setVisibility(View.VISIBLE);
							et_mobile.setBackgroundResource(R.drawable.ed_red);
							tv_comment.setText(jsonObject.optString("comment"));
							dialog.show();
						}
					}
					
					@Override
					public void onError() {
						// TODO Auto-generated method stub
						Log.e("失败", "失败");
					}
				};
				volleryUtil.setShow(true);	
				volleryUtil.setHeaders("token", sha.getString("token", ""));
				volleryUtil.setRequestMethod(1);
				volleryUtil.execute(activity, Constant.URL+"/api/v3/account/transferRaisingrate?", params.toString());
				Log.e("参数", sha.getString("token", ""));
			}
		});
		dialog.show();
		
	}
	private void showUpVersionDialog2(String content, final String url,
			final boolean forceUpdate,String version, final int position) {
		sha = context.getSharedPreferences("config", context.MODE_PRIVATE);
		dialog = new Dialog(context);
		dialog.setCancelable(true);
		Window window = dialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(
				R.color.float_transparent)));
		window.setGravity(Gravity.CENTER);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		window.setContentView(R.layout.get_success);
		TextView tv_get=(TextView)window.findViewById(R.id.tv_upversion_dialog_up);
		TextView get_user=(TextView)window.findViewById(R.id.get_user);
		get_user.setText("已成功转增到"+content+"账户中");		
		ImageView tv_upversion_dialog_clean=(ImageView)window.findViewById(R.id.tv_upversion_dialog_clean);
		tv_upversion_dialog_clean.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				onDialogListener.refresh();
			}
		});
		tv_get.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				onDialogListener.refresh();
			}
		});
		dialog.show();
		
	}
	public interface OnDialogListener{
		public void refresh();
	}
	public void setOnDialopgListener(OnDialogListener OnDialogListener){
		this.onDialogListener=OnDialogListener;
		
	}
}
